package com.yang.servlet;

import com.yang.service.RoleService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/RoleAddServlet")
public class RoleAddServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RoleService roleService = new RoleService();

        try {
            request.setCharacterEncoding("utf-8");
            String roleName = request.getParameter("roleName");
            String roleDesc = request.getParameter("roleDesc");
            String[] menuIds = request.getParameterValues("menuId");
            boolean b = roleService.addRole(roleName, roleDesc, menuIds);
            //4、角色添加成功之后，提示页面prompt.jsp并且提示操作结果
            String tips = b?"<label style='color:green'>添加角色信息成功</label>":"<label style='color:red'>添加角色信息失败</label>";
            request.setAttribute("tips",tips);
            request.getRequestDispatcher("prompt.jsp").forward(request,response);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
